﻿using System;
using System.Collections.Generic;
using System.Linq;
using EnvDTE;
using EnvDTE80;
using Microsoft.SqlServer.Management.UI.VSIntegration;

namespace CnSharp.SSMS.AddIn.Util
{
    public class WindowHelper
    {
        public static Window FindWindowByGuid(string guid)
        {

            var windows = ServiceCache.ExtensibilityModel.Windows;
            for (var i = 1; i <= windows.Count; i++)
            {
                if (windows.Item(i).ObjectKind == guid)
                    return windows.Item(i);
            }

            return null;
        }

        public static Window FindWindowByCaption(string caption)
        {

            var windows = ServiceCache.ExtensibilityModel.Windows;
            for (var i = 1; i <= windows.Count; i++)
            {
                if (windows.Item(i).Caption == caption)
                    return windows.Item(i);
            }

            return null;
        }

        public static Window FindWindow(Func<Window, bool> expression)
        {
            var windowList = new List<Window>();

            var windows = ServiceCache.ExtensibilityModel.Windows;
            for (var i = 1; i <= windows.Count; i++)
            {
                windowList.Add(windows.Item(i));
            }

            return windowList.FirstOrDefault(expression);
        }

        public static object AddWindow(WindowInfo window)
        {
            var myWindow = (Windows2)ServiceCache.ExtensibilityModel.Windows;
            var assemblypath = window.AssemblyLocation;
            var addinobj = ServiceCache.ExtensibilityModel.AddIns.Item(1);


            Object obj = null;
            var newWinobj = myWindow.CreateToolWindow2(addinobj, assemblypath,
                                                       window.ClassName, window.Caption, window.Guid, ref obj);

            newWinobj.Visible = true;
            if(window.Picture != null)
                newWinobj.SetTabPicture(window.Picture);
            //以下两句实现文档式停靠
            if (window.IsDocument)
            {
                newWinobj.IsFloating = false;
                newWinobj.Linkable = false;
            }
            else
            {
                if (window.Width > 0)
                    newWinobj.Width = window.Width;
                if (window.Height > 0)
                    newWinobj.Height = window.Height;
                
                if(window.LinkToWindow != null)
                    myWindow.CreateLinkedWindowFrame(window.LinkToWindow, newWinobj, vsLinkedWindowType.vsLinkedWindowTypeTabbed);
            }


            return obj;
        }


        /// <summary>
        /// output message to Output Window
        /// </summary>
        /// <param name="paneName"></param>
        /// <param name="message"></param>
        public static void WriteToOutputWindow(string paneName, string message)
        {

            var application = ServiceCache.ExtensibilityModel.Application;

            var outputWindow = (OutputWindow)application.Windows.Item("Output").Object;

            OutputWindowPane pane = null;
            try
            {
                pane = outputWindow.OutputWindowPanes.Item(paneName);
            }
            catch
            {
                pane = outputWindow.OutputWindowPanes.Add(paneName);
            }


            outputWindow.Parent.AutoHides = false;
            outputWindow.Parent.Activate();
            pane.Activate();

            // Add a line of text to the new pane.
            pane.OutputString(message + "\r\n");

        }
    }
}
